package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import model.Tgoodtypes;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import dao.GoodTypesDaoI;
import pageModel.EasyuiTreeNode;
import pageModel.GoodTypes;
import service.GoodTypesServiceI;
@Service("goodTypesService")
public class GoodTypesServiceImpl implements GoodTypesServiceI {
private GoodTypesDaoI goodTypesDao;

public GoodTypesDaoI getGoodTypesDao() {
	return goodTypesDao;
}
@Autowired
public void setGoodTypesDao(GoodTypesDaoI goodTypesDao) {
	this.goodTypesDao = goodTypesDao;
}
	    public List<EasyuiTreeNode> allTreeNode(String id) {
		String hql = "from Tgoodtypes t where t.tgoodtypes is null ";
		if (id != null && !id.trim().equals("")) {
			hql = "from Tgoodtypes t where t.tgoodtypes.id = '" + id + "'";
		}
		List<Tgoodtypes> l = goodTypesDao.find(hql);
		List<EasyuiTreeNode> tree = new ArrayList<EasyuiTreeNode>();
		for(Tgoodtypes t : l){
			tree.add(tree(t, false));
		}
		return tree;
	}
	
	private EasyuiTreeNode tree(Tgoodtypes t, boolean recursive) {
		EasyuiTreeNode node = new EasyuiTreeNode();
		node.setId(t.getId());
		node.setText(t.getName());
		if(t.getTgoodtypeses()!=null&&t.getTgoodtypeses().size()>0){
			node.setState("closed");
			if(recursive){
				List<Tgoodtypes> goodtypesList=new ArrayList<Tgoodtypes>(t.getTgoodtypeses());
				List<EasyuiTreeNode> children=new ArrayList<EasyuiTreeNode>();
				for(Tgoodtypes tg : goodtypesList){
					EasyuiTreeNode n=tree(tg,true);
					children.add(n);
				}
				node.setChildren(children);
			}
		}
		return node;
	}
	@Override
	public List<GoodTypes> treegrid(String id) {
		String hql = "from Tgoodtypes t where t.tgoodtypes is null ";
		if (id != null && !id.trim().equals("")) {
			hql = "from Tgoodtypes t where t.tgoodtypes.id = '" + id + "'";
		}
		List<Tgoodtypes> l = goodTypesDao.find(hql);
		List<GoodTypes> nl = new ArrayList<GoodTypes>();
		if (l != null && l.size() > 0) {
			for (Tgoodtypes t : l) {
				GoodTypes g=new GoodTypes();
				BeanUtils.copyProperties(t, g);
				if (t.getTgoodtypes() != null) {
					g.setPid(t.getTgoodtypes().getId());
					g.setPname(t.getTgoodtypes().getName());
				}
				if(t.getTgoodtypeses()!=null&&t.getTgoodtypeses().size()>0){
					g.setState("closed");
				}
				nl.add(g);
			}
		}
		return nl;
	}
	@Override
	public GoodTypes save(GoodTypes goodtypes) {
		   Tgoodtypes u=new  Tgoodtypes();
		   BeanUtils.copyProperties(goodtypes,u);
		   u.setId(UUID.randomUUID().toString());
		   if(goodtypes.getTypeid()!=null){
			   Tgoodtypes t=goodTypesDao.get(Tgoodtypes.class,goodtypes.getTypeid());
			   if(t!=null){
				   u.setTgoodtypes(t);
			   }
		   }
		   goodTypesDao.save(u);
		   BeanUtils.copyProperties(u, goodtypes);
		   return goodtypes;
	}
	@Override
	public GoodTypes edit(GoodTypes goodtypes) {
		Tgoodtypes g = goodTypesDao.get(Tgoodtypes.class, goodtypes.getId());
		if (g != null) {
			String hql="from Tgoodtypes t where t.id=:id or t.name=:name";
			Map<String,Object> params=new HashMap<String,Object>();
			params.put("id", goodtypes.getTypeid());
			params.put("name", goodtypes.getTypeid());
			Tgoodtypes tg = goodTypesDao.get(hql,params);
			BeanUtils.copyProperties(goodtypes, g);
			if (!g.getId().equals("0")) {// 根节点不可以修改上级节点
				g.setTgoodtypes(tg);
			}
			BeanUtils.copyProperties(g, goodtypes);
		}
		return goodtypes;
  }
	@Override
	public void remove(String id) {
		Tgoodtypes g = goodTypesDao.get(Tgoodtypes.class, id);
		this.del(g);
	}
	private void del(Tgoodtypes g) {
		if (g.getTgoodtypeses() != null && g.getTgoodtypeses().size() > 0) {
			Set<Tgoodtypes> gs = g.getTgoodtypeses();
			for (Tgoodtypes t : gs) {
				del(t);
			}
		}
		goodTypesDao.delete(g);
	}
}